RAMDISK = #-DRAMDISK=512

AS	=as --gstabs --32
LD	=ld -m elf_i386 -Ttext 0x0000 -g
LDFLAGS	=-s -x #-M
CC	=gcc $(RAMDISK)
CFLAGS	=-m32 -Wall -O -fstrength-reduce -fomit-frame-pointer -Wno-main \
	-fno-stack-protector -fno-builtin -O0
CPP	=cpp -nostdinc -Iinclude

### 指定根文件系统设备和交换分区系统设备 
ROOT_DEV=/dev/hd6
SWAP_DEV=/dev/hd2

ARCHIVES=kernel/kernel.o mm/mm.o fs/fs.o
DRIVERS =kernel/blk_drv/blk_drv.a kernel/chr_drv/chr_drv.a
MATH	=kernel/math/math.a
LIBS	=

### 指定规则 .c -> .s; .s -> .o; .c -> .o
.c.s:
	$(CC) $(CFLAGS) \
	-nostdinc -Iinclude -S -o $*.s $<
.s.o:
	$(AS) -c -o $*.o $<
.c.o:
	$(CC) $(CFLAGS) \
	-nostdinc -Iinclude -c -o $*.o $<

all:	Image

Image: boot/boot boot/setup system
	@rm -f Image
	@dd if=/dev/zero of=Image bs=512 count=2880
	@dd if=boot/boot of=Image bs=512 count=1 conv=notrunc
	@dd if=boot/setup of=Image bs=512 seek=1 conv=notrunc
	@dd if=system of=Image bs=512 seek=5 conv=notrunc

boot/head.o: boot/head.s

boot/setup: boot/setup.s
	$(AS) -o boot/setup.o boot/setup.s
	$(LD) -s -o boot/setup.elf boot/setup.o
	@objcopy -O binary boot/setup.elf boot/setup

boot/boot: boot/boot.s
	$(AS) -o boot/boot.o boot/boot.s
	$(LD) -s -o boot/boot.elf boot/boot.o
	@objcopy -O binary boot/boot.elf boot/boot

system: boot/head.o init/main.o \
	$(ARCHIVES) $(MATH) $(DRIVERS)
	$(LD) $(LDFLAGS) -o boot/head.elf boot/head.o init/main.o \
		$(ARCHIVES) \
		$(MATH) \
		$(DRIVERS)
	@objcopy -O binary boot/head.elf system

kernel/math/math.a:
	(cd kernel/math; make)

kernel/blk_drv/blk_drv.a:
	(cd kernel/blk_drv; make)
	
kernel/chr_drv/chr_drv.a:
	(cd kernel/chr_drv; make)

kernel/kernel.o:
	(cd kernel; make)

mm/mm.o:
	(cd mm; make)

fs/fs.o:
	(cd fs; make)

clean:
	@rm -f Image
	@rm -f boot/boot boot/boot.o boot/boot.elf
	@rm -f boot/setup boot/setup.o boot/setup.elf
	@rm -f boot/head.o boot/head.elf system
	@rm -f init/main.o
	(cd kernel;make clean)
	(cd mm;make clean)
	(cd fs;make clean)

dep:
	sed '/\#\#\# Dependencies/q' < Makefile > tmp_make
	(for i in init/*.c;do echo -n "init/";$(CPP) -M $$i;done) >> tmp_make
	cp tmp_make Makefile
	(cd fs; make dep)
	(cd kernel; make dep)
	(cd mm; make dep)

### main.o的依赖:
init/main.o : init/main.c include/stdarg.h include/string.h include/linux/mm.h 